﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Model.Utility
{
   public static class Utility
    {
      static Dictionary<string,string> Entries = new Dictionary<string, string>();
       /// <summary>
       /// 
       /// </summary>
       /// <param name="file"></param>
       /// <returns></returns>
       private static string GetKeyForFile(string file)
       {  
           using (FileStream inFile = new FileStream(file, FileMode.Open))
           {
               long fileLength = new FileInfo(file).Length;
               byte[] buf = new byte[fileLength];

               int readLength = 1024;
               if (readLength > fileLength)
                   readLength = (int)fileLength;

               inFile.Read(buf, 0, readLength);
               int hashCode = ByteArrayHashCode(buf);

               return String.Format("{0} {1}", fileLength, hashCode);
           }
       }
       public static bool CheckDirectory(string path)
       {
           //foreach (string file in Directory.GetFiles(path))
           //{
               string key = GetKeyForFile(path);

               

               if (Entries.ContainsKey(key))
               {
                   
                   return true;
               }
               else
               {


                   Entries[key] = key;

                   return false;
               }
               
           //}
           

           //foreach (string subDirectory in Directory.GetDirectories(path))
           //{
           //    CheckDirectory(Path.Combine(path, subDirectory));
           //}
           
       }
       private static int ByteArrayHashCode(params byte[] data)
       {
           unchecked
           {
               const int p = 16777619;
               int hash = (int)2166136261;

               for (int i = 0; i < data.Length; i++)
                   hash = (hash ^ data[i]) * p;

               hash += hash << 13;
               hash ^= hash >> 7;
               hash += hash << 3;
               hash ^= hash >> 17;
               hash += hash << 5;
               return hash;
           }
       }
    }
}
